package com.itxiaowu.quartz;

import com.itxiaowu.constant.RedisConstant;
import com.itxiaowu.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itxiaowu.constant.RedisConstant;
import com.itxiaowu.service.AdjustOrderSettingService;
import com.itxiaowu.utils.DateUtils;
import com.itxiaowu.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 清除定时任务
 */

@Component
public class ClearQuartz {

    @Reference
    private AdjustOrderSettingService adjustOrderSettingService;
    @Autowired
    private JedisPool jedisPool;

    public void clearImage(){
        //根据jedis中保存的两个set集合进行差值计算,获得垃圾图片的名称集合
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        System.out.println("清除任务开始");
        if(set != null){
            for (String s : set) {
                System.out.println("任务一：删除文件");
                System.out.println("删除文件的名称是:" + s);

                //调用七牛的删除方法
                QiniuUtils.deleteFileFromQiniu(s);
                //冲redis中删除
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,s);
            }
        }
    }

    //获得当前的时间，并删除当前月第一天前的预约数据
    public void DeleteOrderSetting()throws Exception{
            //获取当前月第一天时间
        System.out.println("任务二：定时清理预约信息");

        String date = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //传递参数
         adjustOrderSettingService.delete(date);
    }

}
